<template>
  <div class="articles-add-wrapper">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="ruleForm.title" placeholder="请输入文章标题"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <quill-editor
          :content="ruleForm.content"
          :options="editorOption"
          @change="onEditorChange"
          @blur="onEditorBlur"
        ></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="ruleForm.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="btn-wrap">
          <el-button @click="onClose">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// vue-quill-editor 样式引入
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { quillEditor } from "vue-quill-editor";
import { add, update } from "@/api/hmmm/articles.js";

export default {
  components: {
    quillEditor,
  },
  props: {
    // 是否为编辑模式
    value: {
      type: Boolean,
      required: true,
    },
    articleData: Object,
  },
  data() {
    return {
      ruleForm: {
        title: "",
        content: "",
        videoURL: "",
      },
      rules: {
        title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入文章内容" }],
      },
      editorOption: {
        // some quill options
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"], // toggled buttons
            [{ list: "ordered" }, { list: "bullet" }],
            ["blockquote"],
            ["code-block", "image", "link"],
          ],
        },
      },
    };
  },
  mounted() {
    this.fillData(); // 编辑模式回填数据
  },
  methods: {
    async addArticle() {
      try {
        const data = {
          title: this.ruleForm.title,
          articleBody: this.ruleForm.content,
        };
        this.ruleForm.videoURL && (data.videoURL = this.ruleForm.videoURL);
        const res = await add(data);
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    async editArticle() {
      try {
        const data = {
          id: this.articleData.id,
          title: this.ruleForm.title,
          articleBody: this.ruleForm.content,
        };
        this.ruleForm.videoURL && (data.videoURL = this.ruleForm.videoURL);
        const res = await update(data);
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.value) {
            await this.editArticle();
          } else {
            await this.addArticle();
          }
          this.$message({
            message: (this.value ? "修改" : "新增") + "文章成功",
            type: "success",
          });
          this.onClose();
          this.$emit("refreshData");
        } else {
          return false;
        }
      });
    },
    fillData() {
      if (this.value) {
        this.ruleForm.title = this.articleData.title;
        this.ruleForm.content = this.articleData.articleBody;
        this.articleData.videoURL &&
          (this.ruleForm.videoURL = this.articleData.videoURL);
      }
    },
    onEditorChange({ html }) {
      this.ruleForm.content = html;
    },
    onEditorBlur() {
      this.$refs.ruleForm.validateField("content");
    },
    onClose() {
      this.$emit("update:visible", false);
      this.clear();
    },
    clear() {
      this.$emit("input", false);
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang='less'>
.articles-add-wrapper {
  .btn-wrap {
    display: flex;
    justify-content: flex-end;
  }

  // 富文本框样式调整
  .ql-editor {
    height: 200px;
  }
  .ql-toolbar.ql-snow {
    display: flex;
    align-items: center;
    height: 40px;
  }
  .ql-editor.ql-blank::before {
    content: "";
  }
  .ql-snow .ql-tooltip {
    z-index: 3;
  }
}
</style>
